package com.cantrill.ssofilter;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** 
 * Servlet invoked when user clicks logout
 * @author subin
 *
 */
@SuppressWarnings("serial")
public class LogoutActionServlet extends HttpServlet {

	private static Logger log=Logger.getLogger(LogoutActionServlet.class.getName());
	@Override
	public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException 
	{
		try
		{
			String sess=SSOUtil.getSessionIdFromCookie(req, resp);
			String response=SSOUtil.callApi("invalidatesession", "sessionid", sess);
			if("success".equals(response))
			{
				resp.sendRedirect(SSOFilter.getLoginpage()+"?logout=success");
				
			}
			return;
		}
		catch (Exception e) 
		{
			log.log(Level.INFO, "Session id invalid");
		}
		resp.sendRedirect(SSOFilter.getLoginpage()+"?logout=fail");
	
	}
}
